﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net;
using System.IO;
using Resources;

namespace GT5RemoteMobile
{
    public partial class LegacyLogin : System.Web.UI.Page
    {
        string jsessionid;
        protected void Page_Load(object sender, EventArgs e)
        {
            // need JSESSION ID for PSN login wrapping functionality
            if (!Page.IsPostBack)
            {
                HttpWebRequest request = WebRequest.Create(new Uri("https://store.playstation.com/external/index.vm?returnURL=https://us.gran-turismo.com/us/signin/index.do")) as HttpWebRequest;
                using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
                {
                    jsessionid = response.Headers["Set-Cookie"].Split(';')[0];
                }
            }
        }

        protected void LoginPSN(object sender, EventArgs e)
        {
            HttpWebRequest postLogin = WebRequest.Create(new Uri("https://store.playstation.com/external/login.action")) as HttpWebRequest;
            //ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            postLogin.Method = "POST";
#if(DEBUG)
            //postLogin.Proxy = new WebProxy("localhost", 8888);
#endif
            postLogin.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
            postLogin.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            postLogin.Referer = "https://store.playstation.com/external/index.vm?returnURL=https://us.gran-turismo.com/us/signin/index.do";

            StreamWriter loginStream = new StreamWriter(postLogin.GetRequestStream());
            loginStream.Write("loginName=" + txtLogin.Text.Trim() + "&password=" + txtPassword.Text.Trim() + "&returnURL=https%3A%2F%2Fus.gran-turismo.com%2Fus%2Fsignin%2Findex.do");
            loginStream.Close();

            string newUrl = "";
            string sessionId;
            using (HttpWebResponse response = postLogin.GetResponse() as HttpWebResponse)
            {
                sessionId = response.ResponseUri.Query.Substring(1);
                newUrl = "https://us.gran-turismo.com/us/signin/signin.do?" + sessionId + "&url=https%3A%2F%2Fus.gran-turismo.com%2Fus%2F";
            }

            HttpWebRequest cookieRequest = WebRequest.Create(newUrl) as HttpWebRequest;
#if(DEBUG)
            //cookieRequest.Proxy = new WebProxy("localhost", 8888);
#endif
            cookieRequest.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
            cookieRequest.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            cookieRequest.Headers.Add("Cookie", jsessionid);

            using (HttpWebResponse response = cookieRequest.GetResponse() as HttpWebResponse)
            {
                jsessionid = response.Headers["Set-Cookie"].Split(';')[0];
                Session.Add("jsessionid", jsessionid);
            }

            HttpWebRequest sessionRequest2 = WebRequest.Create(new Uri("https://us.gran-turismo.com/us/")) as HttpWebRequest;
#if(DEBUG)
            //sessionRequest2.Proxy = new WebProxy("localhost", 8888);
#endif
            sessionRequest2.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
            sessionRequest2.Referer = newUrl;
            sessionRequest2.Headers.Add("Cookie", jsessionid);

            using (HttpWebResponse response = sessionRequest2.GetResponse() as HttpWebResponse)
            {
            }

            HttpWebRequest sessionLogin = WebRequest.Create(new Uri("https://store.playstation.com/external/login.action")) as HttpWebRequest;
            //ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
            sessionLogin.Method = "POST";
#if(DEBUG)
            //sessionLogin.Proxy = new WebProxy("localhost", 8888);
#endif
            sessionLogin.Accept = "text/javascript, text/html, application/xml, text/xml, */*";
            sessionLogin.ContentType = "application/x-www-form-urlencoded; charset=UTF-8";
            sessionLogin.Referer = "https://store.playstation.com/external/index.vm?returnURL=" + Settings.appUrl;

            StreamWriter sessionStream = new StreamWriter(sessionLogin.GetRequestStream());
            sessionStream.Write("loginName=" + txtLogin.Text.Trim() + "&password=" + txtPassword.Text.Trim() + "&returnURL=" + HttpUtility.UrlEncode(Settings.appUrl));
            sessionStream.Close();

            newUrl = "";
            sessionId = "";
            using (HttpWebResponse response = sessionLogin.GetResponse() as HttpWebResponse)
            {
                sessionId = response.ResponseUri.Query.Substring(1);
                Response.Redirect("landing.aspx?" + sessionId);
            }
        }
    }
}